package com.zhiwu.uhf.ui.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class LoadingView extends View {

    private Paint paint;
    private RectF rectF;
    private int sweepAngle = 0;
    private boolean isRunning = false;

    public LoadingView(Context context, AttributeSet attributes) {
        super(context, attributes);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        rectF = new RectF(100, 100, 500, 500);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectF, 0, sweepAngle, false, paint);
        if (isRunning) {
            sweepAngle += 5;
            if (sweepAngle > 360) {
                sweepAngle = 0;
            }
            postInvalidateDelayed(10);
        }
    }

    public void start() {
        isRunning = true;
        postInvalidate();
    }

    public void stop() {
        isRunning = false;
    }
}